package com.lzy.spring.proxy;

import sun.misc.ProxyGenerator;

import java.io.FileOutputStream;
import java.lang.reflect.Proxy;

public class Test {

    public static void main(String[] args) {
        Man man = new Lzy();
        ManHandler manHandler = new ManHandler(man);
        Man proxyMan = (Man) Proxy.newProxyInstance(man.getClass().getClassLoader(), new Class[]{Man.class}, manHandler);
        System.out.println(proxyMan.getClass());
        System.out.println(proxyMan.getClass().getName());
        System.out.println(proxyMan.getClass().getClassLoader());
        System.out.println(man.getClass().getClassLoader());

        proxyMan.eatObject();

        createProxyClassFile(Man.class);
    }

    private static void createProxyClassFile(Class c){
        byte[] data = ProxyGenerator.generateProxyClass("$Proxy0", new Class[]{c});
        try{
            FileOutputStream fileOutputStream = new FileOutputStream("$Proxy0.class");
            fileOutputStream.write(data);
            fileOutputStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
